package models;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

import play.cache.Cache;
import net.ufrog.common.utils.Strings;

/**
 * 数据模型
 * 
 * @author ultrafrog
 * @version 1.0, 2014-11-28
 * @since 1.0
 */
@Entity
@Table(name = "uwx_clob")
public class Clob extends Model {

	private static final String CACHE_PREFIX	= "play_weixin_cache_clob_";
	
	/** 内容 */
	@Column(name = "lt_text")
	public String text;
	
	/**
	 * 读取数据
	 * 
	 * @param id
	 * @return
	 */
	public static String get(String id) {
		if (!Strings.uuid(id)) return id;
		String text = Cache.get(CACHE_PREFIX + id, String.class);
		if (text == null) {
			text = Clob.class.cast(Clob.findById(id)).text;
			Cache.add(CACHE_PREFIX + id, text);
		}
		return text;
	}
	
	/**
	 * 保存数据
	 * 
	 * @param id
	 * @param text
	 * @return
	 */
	public static String save(String id, String text) {
		Clob clob = Strings.empty(id) ? new Clob() : Clob.class.cast(Clob.findById(id));
		clob.text = text;
		clob.save();
		
		Cache.delete(CACHE_PREFIX + clob.id);
		return clob.id;
	}
}
